<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 按单词下标区间翻转文章内容（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>给定一段英文文章片段&#xff0c;由若干单词组成&#xff0c;单词间以空格间隔&#xff0c;单词下标从0开始。<br /> 请翻转片段中指定区间的单词顺序并返回翻转后的内容。</p> 
<p>例如给定的英文文章片段为&#34;I am a developer&#34;&#xff0c;翻转区间为[0,3]&#xff0c;则输出“developer a am I”。</p> 
<p>String reverseWords(String s, int start, int end)</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>使用换行隔开三个参数</p> 
<ul><li>第一个参数为英文文章内容即英文字符串</li><li>第二个参数为待翻转内容起始单词下标</li><li>第三个参数为待翻转内容最后一个单词下标</li></ul> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>翻转后的英文文章片段所有单词之间以一个半角空格分隔进行输出。</p> 
<p></p> 
<h4>备注</h4> 
<p>英文文章内容首尾无空格</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">I am a developer.<br /> 1<br /> 2</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">I a am developer.</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">hello world<br /> -1<br /> 1</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">world hello</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">下标小于0时&#xff0c;从第一个单词开始</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">I am a developer<br /> 0<br /> 5</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">developer a am I</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">下标大于实际单词个数&#xff0c;则按最大下标算</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">I am a developer<br /> -2<br /> -1</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">I am a developer</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">翻转区间无效时&#xff0c;不做翻转</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题主要考察数组常规操作&#xff0c;以及简单的双指针使用。</p> 
<p>注意startIndex和endIndex的越界处理即可。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61; 3) {
    const s &#61; lines[0];
    const startIndex &#61; lines[1] - 0;
    const endIndex &#61; lines[2] - 0;
    console.log(getResult(s, startIndex, endIndex));
    lines.length &#61; 0;
  }
});

function getResult(s, startIndex, endIndex) {
  const sArr &#61; s.split(&#34; &#34;);

  startIndex &#61; Math.min(Math.max(0, startIndex), sArr.length - 1);
  endIndex &#61; Math.min(Math.max(0, endIndex), sArr.length - 1);

  if (startIndex &gt;&#61; endIndex) return s;

  let l &#61; startIndex;
  let r &#61; endIndex;

  while (l &lt; r) {
    const tmp &#61; sArr[l];
    sArr[l] &#61; sArr[r];
    sArr[r] &#61; tmp;
    l&#43;&#43;;
    r--;
  }

  return sArr.join(&#34; &#34;);
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;
import java.util.StringJoiner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    String s &#61; sc.nextLine();
    int startIndex &#61; Integer.parseInt(sc.nextLine());
    int endIndex &#61; Integer.parseInt(sc.nextLine());
    System.out.println(getResult(s, startIndex, endIndex));
  }

  public static String getResult(String s, int startIndex, int endIndex) {
    String[] sArr &#61; s.split(&#34; &#34;);

    startIndex &#61; Math.min(Math.max(0, startIndex), sArr.length - 1);
    endIndex &#61; Math.min(Math.max(0, endIndex), sArr.length - 1);

    if (startIndex &gt;&#61; endIndex) return s;

    int l &#61; startIndex;
    int r &#61; endIndex;
    while (l &lt; r) {
      String tmp &#61; sArr[l];
      sArr[l] &#61; sArr[r];
      sArr[r] &#61; tmp;
      l&#43;&#43;;
      r--;
    }

    StringJoiner sj &#61; new StringJoiner(&#34; &#34;);
    for (String t : sArr) sj.add(t);
    return sj.toString();
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
s &#61; input()
startIndex &#61; int(input())
endIndex &#61; int(input())


# 算法入口
def getResult(s, start, end):
    sArr &#61; s.split()

    start &#61; min(max(0, start), len(sArr) - 1)
    end &#61; min(max(0, end), len(sArr) - 1)

    if start &gt;&#61; end:
        return s

    l &#61; start
    r &#61; end

    while l &lt; r:
        sArr[l], sArr[r] &#61; sArr[r], sArr[l]
        l &#43;&#61; 1
        r -&#61; 1

    return &#34; &#34;.join(sArr)


# 调用算法
print(getResult(s, startIndex, endIndex))
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

#define MIN(a,b) (a) &lt; (b) ? (a) : (b)
#define MAX(a,b) (a) &gt; (b) ? (a) : (b)

// 第一行输入的最大长度
#define MAX_INPUT_LINE_LENGTH 100000

// 动态数组初始容量
#define DEFAULT_CAPACITY 16

// 动态数组元素
typedef char* E;

// 动态数组结构体
typedef struct ArrayList {
    int size;
    int capacity;
    E* array;
} ArrayList;

// 创建动态数组
ArrayList* new_ArrayList();
// 向动态数组中加入元素
void add_ArrayList(ArrayList* list, E ele);

// 按指定分隔符delimiter分割字符串source
ArrayList* split(char* source, char* delimiter);
// 按指定分隔符delimiter拼接list中的元素
char* join(ArrayList* list, char* delimiter, int container_size);

char* reverseWords(char* s, int start, int end);

int main() {
    char s[MAX_INPUT_LINE_LENGTH];
    scanf(&#34;%[^\n]&#34;, s);

    int start, end;
    scanf(&#34;%d %d&#34;, &amp;start, &amp;end);

    printf(&#34;%s&#34;, reverseWords(s, start, end));

    return 0;
}

char* reverseWords(char* s, int start, int end) {
    ArrayList * list &#61; split(s, &#34; &#34;);

    start &#61; MIN(MAX(0, start), list-&gt;size - 1);
    end &#61; MIN(MAX(0, end), list-&gt;size - 1);

    if(start &gt;&#61; end) return s;

    int l &#61; start;
    int r &#61; end;

    while (l &lt; r) {
        char* tmp &#61; list-&gt;array[l];
        list-&gt;array[l] &#61; list-&gt;array[r];
        list-&gt;array[r] &#61; tmp;
        l&#43;&#43;;
        r--;
    }

    return join(list, &#34; &#34;, MAX_INPUT_LINE_LENGTH);
}


ArrayList* split(char* source, char* delimiter) {
    // strtok 会改变字符串&#xff0c;因此这里对字符串进行拷贝
    char tmp[MAX_INPUT_LINE_LENGTH];
    strcpy(tmp, source);

    ArrayList* list &#61; new_ArrayList();

    char* token &#61; strtok(tmp, delimiter);
    while (token !&#61; NULL) {
        add_ArrayList(list, token);
        token &#61; strtok(NULL, delimiter);
    }

    return list;
}

char* join(ArrayList* list, char* delimiter, int container_size) {
    char* container &#61; (char*) calloc(container_size, sizeof(char));

    for(int i&#61;0; i&lt;list-&gt;size; i&#43;&#43;) {
        char* ele &#61; list-&gt;array[i];

        strcat(container, ele);

        if(i !&#61; list-&gt;size - 1) {
            strcat(container, delimiter);
        }
    }

    return container;
}

ArrayList* new_ArrayList() {
    ArrayList* list &#61; (ArrayList*) malloc(sizeof(ArrayList));
    list-&gt;size &#61; 0;
    list-&gt;capacity &#61; DEFAULT_CAPACITY;
    list-&gt;array &#61; (E*) malloc(sizeof(E) * DEFAULT_CAPACITY);

    for(int i&#61;0; i&lt;DEFAULT_CAPACITY; i&#43;&#43;) {
        list-&gt;array[i] &#61; NULL;
    }

    return list;
}

void add_ArrayList(ArrayList* list, E ele) {
    if(list-&gt;size &#61;&#61; list-&gt;capacity) {
        int newCapacity &#61; list-&gt;capacity &lt;&lt; 1;

        void* p &#61; realloc(list-&gt;array, sizeof(E) * newCapacity);
        if(p &#61;&#61; NULL) exit(-1);

        list-&gt;array &#61; (E*) p;
        list-&gt;capacity &#61; newCapacity;
    }

    list-&gt;array[list-&gt;size] &#61; ele;
    list-&gt;size&#43;&#43;;
}</code></pre> 
<p> </p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>